/*
 *MIT License
 *Copyright (c) 2021 fullset
 *@ClassName SysRoleServiceImpl
 *@date 2021/7/10 10:06
 *@author: pangps
 *@Description TODO
 */
package com.fs.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fs.base.service.SysRoleService;
import com.fs.core.exception.BizException;
import com.fs.core.exception.BizExceptionEnum;
import com.fs.core.util.BeanUtil;
import com.fs.orm.dao.SysRoleDao;
import com.fs.orm.entity.SysRole;
import com.fs.orm.entity.SysUser;
import com.fs.orm.vo.form.SysRoleForm;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Override
    public SysRoleForm addRole(SysRoleForm sysRoleForm) throws Exception {
        SysRole sysRole = new SysRole();
        BeanUtil.copyAttribute(sysRoleForm, sysRole);
        boolean result;
        try {
            result = sysRoleDao.addRole(sysRole);
        }catch (Exception e){
            throw new Exception("sql语法错误!");
        }
        if(!result){
            throw new BizException(BizExceptionEnum.SYS_ROLE_ADD_ERROR);
        }
        return sysRoleForm;
    }

    @Override
    public SysRoleForm editRole(SysRoleForm sysRoleForm) {
        SysRole sysRole = new SysRole();
        BeanUtil.copyAttribute(sysRoleForm, sysRole);
        try {
            sysRole = sysRoleDao.editRole(sysRole);

        }catch (Exception e){
            // throw new Exception("sql语法错误!");
        }
        return sysRoleForm;
    }

    @Override
    public List<SysRole> getAll() {
        return null;
    }

    @Override
    public PageInfo<SysRole> getLayTable(Map<String, Object> params) {
        PageHelper.startPage(Integer.valueOf(String.valueOf(params.get("page"))),Integer.valueOf(String.valueOf(params.get("limit")))) ;
        QueryWrapper qWrapper = new QueryWrapper();
        qWrapper.eq("is_deleted", 0);
        qWrapper.orderByAsc("id");
        List<SysRole> list = sysRoleDao.getList(qWrapper);
        PageInfo<SysRole> rolePage =new PageInfo<>(list);
        return rolePage;
    }

    @Override
    public SysRoleForm initForm() {
        return null;
    }

    @Override
    public SysRoleForm editForm(Long id) {
        SysRole sysRole = sysRoleDao.getById(id);
        SysRoleForm sysRoleForm = new SysRoleForm();
        BeanUtil.copyAttribute(sysRole, sysRoleForm);
        return sysRoleForm;
    }

    @Override
    public boolean logicalDeleteById(String id) {
        return sysRoleDao.logicalDeleteById(id);
    }

    @Override
    public boolean batchDel(String ids) {
        return sysRoleDao.batchDel(ids);
    }
}
